#include <SFML/Graphics.hpp>
#include <time.h>
using namespace sf;

int N = 30, M = 20;
int size = 16;
int w = size * N;
int h = size * M;

int dir, num = 2;

struct Point
{
	int x, y;
} s[100], f; // 蛇，果子

void Tick() // 移动
{
	for (int i = num; i > 0; --i)
	{
		s[i].x = s[i - 1].x;
		s[i].y = s[i - 1].y;
	}
	if (dir == 0)
		s[0].y += 1; // 下
	if (dir == 1)
		s[0].x -= 1; // 左
	if (dir == 2)
		s[0].x += 1; // 右
	if (dir == 3)
		s[0].y -= 1; // 上

	if ((s[0].x == f.x) && (s[0].y == f.y))
	{
		num++;
		f.x = rand() % N;
		f.y = rand() % M;
	}

	// 边界穿越
	if (s[0].x > N)
		s[0].x = 0;
	if (s[0].x < 0)
		s[0].x = N;
	if (s[0].y > M)
		s[0].y = 0;
	if (s[0].y < 0)
		s[0].y = M;

	// 碰撞
	for (int i = 1; i < num; i++)
		if (s[0].x == s[i].x && s[0].y == s[i].y)
			num = i;
}

int main()
{
	srand(time(0));

	RenderWindow window(VideoMode(w + size, h + size), "Snake Game!--YzHCat");

	Texture b1, b2, s1, s2, s3, s4;
	b1.loadFromFile("../images/background.png");
	b2.loadFromFile("../images/b.png");
	s1.loadFromFile("../images/a.png");
	s2.loadFromFile("../images/green.png");
	s3.loadFromFile("../images/hat.png");
	s4.loadFromFile("../images/white.png");

	Sprite s_ba, s_bl(s1);

	// 加载字体
	Font font;
	if (!font.loadFromFile("../MI_LanTing_Regular.ttf"))
		return -1;
	sf::String txt = L"↑↓更换背景\n←→更换皮肤\nEnter 开始游戏";
	Text text(txt, font);
	// 设置字体大小
	text.setCharacterSize(30);
	// 设置填充颜色
	text.setFillColor(Color::Cyan);
	// 位置
	text.setPosition(0, h / 2 - 100);

	Clock clock;
	float timer = 0, delay = 0.1;

	f.x = 1;
	f.y = 20;

	int run = 1;
	int strat = 0;
	int ba_key = 1;
	int bl_key = 1;
	while (window.isOpen())
	{
		float time = clock.getElapsedTime().asSeconds();
		clock.restart();
		timer += time;

		Event e;
		while (window.pollEvent(e))
		{
			if (e.type == Event::Closed)
				window.close();
			if (e.type == Event::KeyPressed)
			{
				if (e.key.code == Keyboard::Escape)
					window.close();
				if (e.key.code == Keyboard::Space)
					run = 1 - run;
				if (!strat)
				{
					if (e.key.code == Keyboard::Left)
						bl_key++;
					if (e.key.code == Keyboard::Right)
						bl_key--;
					if (e.key.code == Keyboard::Up)
						ba_key++;
					if (e.key.code == Keyboard::Down)
						ba_key--;
					if (e.key.code == Keyboard::Enter)
						strat = 1;
				}
			}
		}
		if (run)
		{
			if (strat)
			{ // 移动
				if (Keyboard::isKeyPressed(Keyboard::Left))
					dir = 1;
				if (Keyboard::isKeyPressed(Keyboard::Right))
					dir = 2;
				if (Keyboard::isKeyPressed(Keyboard::Up))
					dir = 3;
				if (Keyboard::isKeyPressed(Keyboard::Down))
					dir = 0;
			}

			if (timer > delay)
			{
				timer = 0;
				Tick();
			}

			//////// draw ////////
			if (ba_key < 0)
				ba_key = 3;
			if (ba_key > 4)
				ba_key = 1;
			int temp = ba_key % 4;
			switch (temp)
			{
			case 1:
				s_ba.setTexture(b2);
				break;
			case 2:
				s_ba.setTexture(b1);
				break;
			default:
				break;
			}

			if (temp == 3)
				window.clear(Color::White);
			else
				window.clear(Color::Black);
			if (temp == 1 || temp == 2)
				window.draw(s_ba);

			if (bl_key < 0)
				bl_key = 3;
			if (bl_key > 4)
				bl_key = 1;
			temp = bl_key % 4;
			for (int i = 0; i < num; i++)
			{
				// if (i == 0)
				// 	s_bl.setTexture(s3);
				// else
				switch (temp)
				{
				case 1:
					s_bl.setTexture(s1);
					break;
				case 2:
					s_bl.setTexture(s2);
					break;
				case 3:
					s_bl.setTexture(s3);
					break;
				default:
					s_bl.setTexture(s4);
					break;
				}
				s_bl.setPosition(s[i].x * size, s[i].y * size);
				window.draw(s_bl);
			}

			s_bl.setPosition(f.x * size, f.y * size);
			window.draw(s_bl);

			if (!strat)
				window.draw(text);
			window.display();
		}
	}

	return 0;
}
